import 'package:flutter/material.dart';
import 'package:svgaplayer_flutter/svgaplayer_flutter.dart';

class BarItemSvgaIcon extends StatefulWidget {
  final String assetsUrl;
  final bool? repeat;
  const BarItemSvgaIcon({Key? key, required this.assetsUrl, this.repeat}) : super(key: key);

  @override
  State<BarItemSvgaIcon> createState() => _BarItemSvgaIconState();
}

class _BarItemSvgaIconState extends State<BarItemSvgaIcon> with SingleTickerProviderStateMixin{
  late SVGAAnimationController animationController;

  @override
  void initState() {
    animationController = SVGAAnimationController(vsync: this);
    loadAnimation();
    super.initState();
  }

  void loadAnimation() async {
    // decodeFromAssets decodeFromUrl
    final videoItem = await SVGAParser.shared.decodeFromAssets(widget.assetsUrl);
    animationController.videoItem = videoItem;
    widget.repeat == true ? animationController.repeat() : animationController.forward();
    //.whenComplete(() => this.animationController.videoItem = null);
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SVGAImage(animationController,clearsAfterStop:false);
  }
}
